package com.audi.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.audi.R;
import com.audi.dto.DeviceItem;
import com.audi.dto.PlaceItem;
import com.audi.utils.DeviceHelper;
import com.audi.view.AudiDialog;
import com.audi.view.DeviceUpdatePhoto;

public class PlaceDeviceCardAdapter extends BaseAdapter {
	
	private Activity mActivity;
	private ArrayList<DeviceItem> mDeviceList;
	private ArrayList<PlaceItem> mListPlaceCardLDCA;
	private DeviceUpdatePhoto mDeviceUpdatePhoto;
	private SQLiteAsusNewHelper mSQLHelper;
	private int mIndexDeviceList;
	public PlaceDeviceCardAdapter(Activity activity,
			ArrayList<DeviceItem> deviceList,
			DeviceUpdatePhoto deviceUpdatePhoto) {
		mActivity = activity;
		mDeviceList = deviceList;
		mDeviceUpdatePhoto = deviceUpdatePhoto;

	}
	private void callNotifyDataSetChanged(){
		this.notifyDataSetChanged();
	}
	public int getCount() {
		return mDeviceList.size();
	}

	public DeviceItem getItem(int pos) {
		return mDeviceList.get(pos);
	}

	public long getItemId(int pos) {
		return pos;
	}

	public View getView(int i, View convertView, ViewGroup viewgroup) {
		final DeviceItem item = mDeviceList.get(i);
		ViewHolder viewHolderPDCA = null;
		if (convertView == null) {
			viewHolderPDCA = new ViewHolder();
			convertView = LayoutInflater.from(mActivity).inflate(
					R.layout.row_device_card_bot_new, null);
			viewHolderPDCA.rltDrag = (RelativeLayout) convertView
					.findViewById(R.id.rlt_drag);
			viewHolderPDCA.imvDeviceIcon = (ImageView) convertView
					.findViewById(R.id.imv_device_icon);
			viewHolderPDCA.frmUserStatusContainer = (FrameLayout) convertView
					.findViewById(R.id.frm_user_status_container);
			viewHolderPDCA.imvUserAvatar = (ImageView) convertView
					.findViewById(R.id.imv_user_avatar);
			viewHolderPDCA.imvUserStatus = (ImageView) convertView
					.findViewById(R.id.imv_user_status);
			viewHolderPDCA.txtDeviceName = (TextView) convertView
					.findViewById(R.id.txt_device_name);
			viewHolderPDCA.txtPlaceName = (TextView) convertView
					.findViewById(R.id.txt_place_name);
			viewHolderPDCA.imvDeviceMenu = (ImageView) convertView
					.findViewById(R.id.imv_device_menu);
			viewHolderPDCA.imvDeviceTypeIcon = (ImageView) convertView
					.findViewById(R.id.imv_device_type_icon);
			viewHolderPDCA.lnlDeviceTypeText = (LinearLayout) convertView
					.findViewById(R.id.lnl_device_type_text);
			viewHolderPDCA.txtDeviceTextNumber = (TextView) convertView
					.findViewById(R.id.txt_device_text_number);
			viewHolderPDCA.txtDeviceTextUnit = (TextView) convertView
					.findViewById(R.id.txt_device_text_unit);
			convertView.setTag(viewHolderPDCA);
		} else {
			viewHolderPDCA = (ViewHolder) convertView.getTag();
		}

		// set Data for View
		if (item.getDeviceStatus() == 1) {
			viewHolderPDCA.rltDrag.setAlpha(1f);
		} else {
			viewHolderPDCA.rltDrag.setAlpha(0.3f);
		}

		if (TextUtils.isEmpty(item.getDeviceIcon().trim())) {
			viewHolderPDCA.imvDeviceIcon
					.setImageResource(R.drawable.icon_device_default);
		} else {
			BitmapDrawable bitmapDrawable = new BitmapDrawable(
					mActivity.getResources(), item.getDeviceIcon());
			viewHolderPDCA.imvDeviceIcon.setImageDrawable(bitmapDrawable);
		}

		if (item.getShowUserStatus() == 1) {
			viewHolderPDCA.frmUserStatusContainer.setVisibility(View.VISIBLE);
			if (TextUtils.isEmpty(item.getUserIcon().trim())) {
				viewHolderPDCA.imvUserAvatar
						.setImageResource(R.drawable.icon_device_default_user);
			} else {
				BitmapDrawable bitmapDrawable = new BitmapDrawable(
						mActivity.getResources(), item.getUserIcon());
				viewHolderPDCA.imvUserAvatar.setImageDrawable(bitmapDrawable);
			}
			if (item.getUserStatus() == 1) {
				viewHolderPDCA.imvUserStatus
						.setImageResource(R.drawable.circle_at_home);
			} else {
				viewHolderPDCA.imvUserStatus
						.setImageResource(R.drawable.circle_not_at_home);
			}
		} else {
			viewHolderPDCA.frmUserStatusContainer.setVisibility(View.GONE);
		}

		viewHolderPDCA.txtDeviceName.setText(item.getDeviceName());
		viewHolderPDCA.txtPlaceName.setText(item.getDevicePlace());

		if (DeviceHelper.getControlType(item.getDeviceType()) == 1) {
			viewHolderPDCA.lnlDeviceTypeText.setVisibility(View.GONE);
			viewHolderPDCA.imvDeviceTypeIcon.setVisibility(View.VISIBLE);
			viewHolderPDCA.imvDeviceTypeIcon.setImageResource(DeviceHelper
					.getProperty(item.getDeviceType(), item.getStatusValue()));
		} else if (DeviceHelper.getControlType(item.getDeviceType()) == 2) {
			viewHolderPDCA.lnlDeviceTypeText.setVisibility(View.VISIBLE);
			viewHolderPDCA.imvDeviceTypeIcon.setVisibility(View.GONE);
			viewHolderPDCA.txtDeviceTextNumber.setTextColor(DeviceHelper
					.getProperty(item.getDeviceType(), item.getStatusValue()));
			viewHolderPDCA.txtDeviceTextNumber.setText(String.valueOf(item
					.getStatusValue()));
			if (DeviceHelper.hasUnit(item.getDeviceType())) {
				viewHolderPDCA.txtDeviceTextUnit.setVisibility(View.VISIBLE);
				viewHolderPDCA.txtDeviceTextUnit.setText(DeviceHelper
						.getUnit(item.getDeviceType()));
			} else {
				viewHolderPDCA.txtDeviceTextUnit.setVisibility(View.GONE);
			}
		}

		// set Listener for View
		if (item.getDeviceStatus() == 1) {
			viewHolderPDCA.imvDeviceMenu
					.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View imvDeviceMenu) {
							mIndexDeviceList = mDeviceList.indexOf(item);
							PopupMenu popupMenuListDeviceAdapter = new PopupMenu(
									mActivity, imvDeviceMenu);
							popupMenuListDeviceAdapter
									.getMenuInflater()
									.inflate(
											R.menu.popup_menu_sub_device_card_valid,
											popupMenuListDeviceAdapter
													.getMenu());
							popupMenuListDeviceAdapter
									.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
										public boolean onMenuItemClick(
												MenuItem item) {
											if (item.getItemId() == R.id.item_update_photo) {
												// mDeviceUpdatePhoto.updatePhoto();
												setActionOfSubMenuDevices(0);
											}
											if (item.getItemId() == R.id.item_rename) {
												setActionOfSubMenuDevices(1);
												// renameDeviceCard();
											}
											if (item.getItemId() == R.id.item_change_place) {
												setActionOfSubMenuDevices(2);
												// changePlaceDevice();
											}
											if (item.getItemId() == R.id.item_remove_device) {
												setActionOfSubMenuDevices(3);
												// removeDeviceCard();
											}
											return true;
										}
									});
							popupMenuListDeviceAdapter.show();
						}
					});
			if (DeviceHelper.getControlType(item.getDeviceType()) == 1) {
				viewHolderPDCA.imvDeviceTypeIcon
						.setOnClickListener(new OnClickListener() {
							@Override
							public void onClick(View arg0) {
								Toast.makeText(mActivity,
										"OnClick Device Control",
										Toast.LENGTH_SHORT).show();
							}
						});
			}
		}

		return convertView;
	}

	public void setActionOfSubMenuDevices(int flagMenuDevice) {
		switch (flagMenuDevice) {
		case 0:
			mDeviceUpdatePhoto.updatePhoto();
			break;

		case 1:
			final AudiDialog dialogRenameDevice = new AudiDialog(mActivity);
			dialogRenameDevice.setLayout(R.layout.rename_device_dialog);
			dialogRenameDevice.show();
			final EditText txtRenameDevice = (EditText) dialogRenameDevice
					.findViewById(R.id.edt_rename_device_dilog_input);
			txtRenameDevice.setText("");
			txtRenameDevice.setHint("Rename device");
			Button btnRenameDeviceCancel = (Button) dialogRenameDevice
					.findViewById(R.id.btn_rename_device_dialog_skip);
			Button btnRenameDeviceOk = (Button) dialogRenameDevice
					.findViewById(R.id.btn_rename_device_dialog_ok);
			btnRenameDeviceCancel.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					dialogRenameDevice.dismiss();
				}
			});
			btnRenameDeviceOk.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					DeviceItem item = mDeviceList.get(mIndexDeviceList);
				
					mSQLHelper = new SQLiteAsusNewHelper(mActivity);
					item.setDeviceName(txtRenameDevice.getText().toString());
					item.setDeviceIcon(item.getDeviceIcon());
					item.setDeviceStatus(item.getDeviceStatus());
					item.setDeviceType(item.getDeviceType());
					item.setStatusValue(item.getStatusValue());
					item.setDeviceCode(item.getDeviceCode());
					item.setDeviceBoxId(0);
					item.setDevicePlaceId(item.getDevicePlaceId());
					item.setUserId(item.getUserId());
					item.setDeviceBoxName("thanh");
					if (item.getUserIcon() == null) {
						item.setUserIcon("");
					}
					mSQLHelper.updateDeviceTop(item);
					 
					callNotifyDataSetChanged();
					dialogRenameDevice.dismiss();
				}
			});
			dialogRenameDevice.show();
			break;
		case 2:
			BaseEditDeviceDialog mBaseEditDeviceDialog= new BaseEditDeviceDialog(mActivity);
			mBaseEditDeviceDialog.showSelectPlaceDialog();
			break;
		case 3:
			AlertDialog.Builder mListDialog = new AlertDialog.Builder(mActivity);
			mListDialog.setTitle(R.string.remove_this_device_title);
			mListDialog.setMessage(R.string.when_you_remove_the_device);
			mListDialog
					.setCancelable(false)
					.setPositiveButton("Yes",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									DeviceItem item = mDeviceList
											.get(mIndexDeviceList);
									mSQLHelper = new SQLiteAsusNewHelper(
											mActivity);
									item.setDeviceId(item.getDeviceId());
									mSQLHelper.deleteDeviceItem(item);
									mDeviceList.remove(mIndexDeviceList);
									callNotifyDataSetChanged();
								}
							})
					.setNegativeButton("No",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									dialog.cancel();
								}
							}).show();
			break;
		}
	}

	static class ViewHolder {
		public RelativeLayout rltDrag;
		public ImageView imvDeviceIcon;
		public FrameLayout frmUserStatusContainer;
		public ImageView imvUserAvatar;
		public ImageView imvUserStatus;
		public TextView txtDeviceName;
		public TextView txtPlaceName;
		public ImageView imvDeviceMenu;
		public ImageView imvDeviceTypeIcon;
		public LinearLayout lnlDeviceTypeText;
		public TextView txtDeviceTextNumber;
		public TextView txtDeviceTextUnit;

	}
}
